/**   
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-11-24 22:18:20  
 * Copyright (c) 2015- joysuch-版权所有     
 */

package com.joysuch.wwyt.risk.service.impl;

import com.joysuch.wwyt.bp.entity.BpDeviceFacilityCollection;
import com.joysuch.wwyt.bp.repository.BpDeviceFacilityCollectionDao;
import com.joysuch.wwyt.bp.repository.BpJobDao;
import com.joysuch.wwyt.bp.repository.BpProductionFacilityDao;
import com.joysuch.wwyt.core.repository.BaseUserDao;
import com.joysuch.wwyt.core.service.BizCodeGeneratorFacade;
import com.joysuch.wwyt.core.support.Context;
import com.joysuch.wwyt.risk.bean.RiskSclEvaluationBean;
import com.joysuch.wwyt.risk.entity.RiskPoint;
import com.joysuch.wwyt.risk.entity.RiskPointEvaluationScl;
import com.joysuch.wwyt.risk.entity.RiskPointEvaluationSclItem;
import com.joysuch.wwyt.risk.repository.RiskPointEvaluationSclDao;
import com.joysuch.wwyt.risk.service.RiskPointEvaluationSclService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class RiskPointEvaluationSclServiceImpl implements RiskPointEvaluationSclService {

    @Autowired
    private RiskPointEvaluationSclDao dao;
    @Autowired
    private BaseUserDao baseUserDao;
    @Autowired
    private BpJobDao bpJobDao;
    @Autowired
    private BpProductionFacilityDao bpProductionFacilityDao;
    @Autowired
    private BizCodeGeneratorFacade bizCodeGeneratorFacade;
    @Autowired
    private BpDeviceFacilityCollectionDao bpDeviceFacilityCollectionDao;

    @Override
    public void add(RiskPointEvaluationScl entity) throws Exception {
        dao.save(entity);
    }

    @Override
    public void update(RiskPointEvaluationScl entity) throws Exception {
        RiskPointEvaluationScl content = dao.findById(entity.getId()).get();
        if (content != null) {
            // TODO set nessessary properties
            dao.save(entity);
        }
    }

    @Override
    public Page<RiskPointEvaluationScl> findByPage(Pageable page) {
        return dao.findAll(page);
    }

    @Override
    public void delete(Long id) {
        dao.deleteById(id);
    }

    @Override
    public RiskPointEvaluationScl findById(Long id) {
        return dao.findById(id).get();
    }

    @Override
    public RiskPointEvaluationScl doEvaluation(RiskPoint point, RiskSclEvaluationBean evaluationBean) {
        RiskPointEvaluationScl scl = new RiskPointEvaluationScl();
        if (evaluationBean.getAnalyzeTime() == null) {
            scl.setAnalyzeTime(new Date());
        } else {
            scl.setAnalyzeTime(evaluationBean.getAnalyzeTime());
        }
        if (evaluationBean.getAnalyzeUserId() == null) {
            scl.setBaseUser(baseUserDao.getOne(Context.getCurrentUserId()));
        } else {
            scl.setBaseUser(baseUserDao.getOne(evaluationBean.getAnalyzeUserId()));
        }
        scl.setBpJob(
                evaluationBean.getJobId() == null ? null : bpJobDao.findById(evaluationBean.getJobId()).orElse(null));
        if (evaluationBean.getFacilityId() != null) {
            //目前只有生产装置的评估关联
            BpDeviceFacilityCollection collection = bpDeviceFacilityCollectionDao.getOne(evaluationBean.getFacilityId());
            scl.setBpProductionFacility(bpProductionFacilityDao.getOne(collection.getDeviceId()));
        }
        scl.setInControlNo(evaluationBean.getInControlCode());
        Integer riskLevel = findMaxRiskLevel(evaluationBean);
        scl.setRiskLevel(riskLevel);
        scl.setRiskPointId(point.getId());
        scl.getRiskPointEvaluationSclItems().addAll(evaluationBean.getItems());
        // 保存对象
        dao.save(scl);
        return scl;
    }

    private Integer findMaxRiskLevel(RiskSclEvaluationBean evaluationBean) {
        Integer level = Integer.MAX_VALUE;
        // if (evaluationBean.getRiskLevel() != null) {
        // level = evaluationBean.getRiskLevel();
        // }
        List<RiskPointEvaluationSclItem> items = evaluationBean.getItems();
        for (RiskPointEvaluationSclItem t : items) {
            if (t.getRiskLevel() != null && level > t.getRiskLevel()) {
                level = t.getRiskLevel();
            }
        }
        return level == Integer.MAX_VALUE ? null : level;
    }
}
